package Model;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import Hibernate.*;

public class Model 
{
	private static Session session = HibernateUtil.getSessionFactory().openSession();
	
	public Model()
	{
		
	}
	
	public void addUser(User selectedUser)
	{
		Transaction tns = null;
		
		try
		{
			tns = session.beginTransaction();

			session.save(selectedUser);
			
			if(!tns.wasCommitted())
			{
				tns.commit();
			}
		}
		
		catch(RuntimeException e){
			if(tns != null)
			{
				tns.rollback();
			}
			//e.printStackTrace();
		}
	}
	
	public void addComment(Comment selectedComment)
	{
		Transaction tns = null;
		
		try
		{
			tns = session.beginTransaction();

			session.save(selectedComment);
			
			if(!tns.wasCommitted())
			{
				tns.commit();
			}
		}
		
		catch(RuntimeException e){
			if(tns != null)
			{
				tns.rollback();
			}
			//e.printStackTrace();
		}
	}
	
	public void addLink(Link currentLink)
	{
		Transaction tns = null;
		
		try
		{
			tns = session.beginTransaction();

			session.save(currentLink);
			
			if(!tns.wasCommitted())
			{
				tns.commit();
			}
		}
		
		catch(RuntimeException e)
		{
			if(tns != null)
			{
				tns.rollback();
			}
			//e.printStackTrace();
		}
	}

	public void addURL(URL currentURL)
	{
		Transaction tns = null;
		
		try
		{
			tns = session.beginTransaction();

			session.save(currentURL);
			
			if(!tns.wasCommitted())
				session.getTransaction().commit();
		}
		
		catch(RuntimeException e){
			if(tns != null)
			{
				tns.rollback();
			}
		}
	}
	
	public void updateURL(URL currentURL)
	{
		Transaction tns = null;
		
		try
		{
			tns = session.beginTransaction();
			session.update(currentURL);
			if(!tns.wasCommitted())
				session.getTransaction().commit();
		}
		
		catch(RuntimeException e)
		{
			if(tns != null)
				tns.rollback();
		}
	}
	
	public URL getURLObjectForUrl(String currentURL)
	{
		Transaction tns = null;
		URL returnURL = null;
		
		try
		{
			tns = session.beginTransaction();
			List<URL> urlList = session.createQuery("from URL as u where u.url =:myURL").setString("myURL", currentURL).list();
			
			returnURL = (urlList.size() > 0) ? urlList.get(0) : null;
			
			if(!tns.wasCommitted())
			{
				tns.commit();
			}
		}
		
		catch(RuntimeException e)
		{
			if(tns != null)
			{
				tns.rollback();
			}
			
			e.printStackTrace();
		}
		
		return returnURL;	
	}
	
	public static List<URL> getAllURLs()
	{
		Transaction tns = null;
		List<URL> urlList = null;
		
		try
		{
			tns = session.beginTransaction();
			urlList = session.createQuery("from URL").list();
			
			if(!tns.wasCommitted())
			{
				tns.commit();
			}
		}
		
		catch(RuntimeException e)
		{
			if(tns != null)
			{
				tns.rollback();
			}
			
			e.printStackTrace();
		}
		
		return urlList;	
	}
	
	public boolean Exists (Comment instance) 
	{
	    return (session.createQuery("select 1 from Comment c where c.fb_comment_id = :fb_comm_id").setString("fb_comm_id", instance.getFb_comment_id() ).uniqueResult() != null);
	}
	
	public boolean isUser (User instance) 
	{
		Transaction tns = null;
		
		tns = session.beginTransaction();
		List<User> usrList = session.createQuery("from User as usr where usr.poster_id =:myUsr").setString("myUsr", instance.getPoster_id()).list();
			
		if(usrList.size() > 0)
			return true;
		else
			return false;	
	}
}
